package com.live.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.live.adapter.LiveHomeFriendSA;
import com.live.adapter.LiveHomeMsgSA;
import com.live.adapter.LiveNewsSA;
import com.live.common.Constant;
import com.live.common.LiveCommon;
import com.live.common.LiveImageHelper;
import com.live.task.LiveFriend;
import com.live.task.LiveMessage;
import com.live.task.LiveTopic;
import com.live.tools.LiveTools;

public class LiveUserHomeActivity extends LiveBaseActivity {

	private TextView userId;

	private LiveNewsSA newsSA;

	private LiveHomeFriendSA homeFriendSA;

	private LiveHomeMsgSA homeMsgSA;

	private ListView homeMsgLV;

	private ListView homeNewsLV;

	private ListView homeFriendLV;

	private View liveFoot;

	private int msgPage = 0;

	private int msgIndex = 0;

	private int newsPage = 0;

	private int newsIndex = 0;

	private int friendPage = 0;

	private int friendIndex = 0;

	private List<Map<String, Object>> liveHomeMsgList = new ArrayList<Map<String, Object>>();

	private List<Map<String, Object>> liveHomeNewsList = new ArrayList<Map<String, Object>>();

	private List<Map<String, Object>> liveFriendList = new ArrayList<Map<String, Object>>();

	private LinearLayout liveUserHome;
	private RelativeLayout homeNewsArea;
	private LinearLayout homePageArea;
	private LinearLayout homeFriendArea;

	private Bundle bundle;

	protected void onResume() {
		super.onResume();

		// 设置用户信息
		if (findViewById(R.id.homePageArea) != null) {
			// 设置名称
			userId = (TextView) findViewById(R.id.userId);
			userId.setText(store.getString("userId", null));

			// 设置头像
			ImageView userPic = (ImageView) findViewById(R.id.userPic);
			if (Constant.Y.equals(store.getString("userPic", null))) {
				LiveImageHelper.loadBitMap(userPic, store.getString("id", null), Constant.NUM_ZERO);
			}

			// 设置邮箱
			if (store.getString("userMail", null) != null && !"null".equals(store.getString("userMail", null))) {
				TextView userMail = (TextView) findViewById(R.id.userMail);
				userMail.setText(store.getString("userMail", null));
			}

			// 设置生日
			TextView userBirthday = (TextView) findViewById(R.id.userBirthday);
			userBirthday.setText(store.getString("userBirthday", null));

			// 设置性别
			TextView userSex = (TextView) findViewById(R.id.userSex);
			if (Constant.USER_SEX_MAN.equals(store.getString("userSex", null))) {
				userSex.setText("♂");
				userSex.setTextColor(0xff00CCFF);
				userBirthday.setTextColor(0xff00CCFF);
			} else {
				userSex.setText("♀");
				userSex.setTextColor(0xffFF6699);
				userBirthday.setTextColor(0xffFF6699);
			}

			// 设置签名
			if (store.getString("userSign", null) != null && !"null".equals(store.getString("userSign", null))) {
				TextView userSign = (TextView) findViewById(R.id.userSign);
				userSign.setText(store.getString("userSign", null));
			}

			Button userSettingBtn = (Button) findViewById(R.id.userSettingBtn);
			userSettingBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(context, LiveUserSettingActivity.class);
					startActivityForResult(intent, Constant.REQUEST_CODE);
				}
			});
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.live_user_home);

		/* 获取Intent中的Bundle对象 */
		bundle = new Bundle();
		bundle.putString("uId", store.getString("id", null));
		bundle.putString("userId", store.getString("userId", null));
		bundle.putString("self", "0");

		// 设置头部信息
		LiveCommon.setHead(context, (LinearLayout) findViewById(R.id.liveHead), R.string.title_my_area,
				R.string.issue_btn);

		// 设置头部按钮（发布新鲜事）
		Button sendMsgBtn = (Button) findViewById(R.id.headFuncBtn);
		sendMsgBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context, LiveTopicActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("userId", store.getString("id", null));
				intent.putExtras(bundle);
				startActivityForResult(intent, Constant.REQUEST_CODE);
			}
		});

		// 取得留言提醒VIEW
		homeMsgLV = (ListView) findViewById(R.id.homeMsgList);
		// 取得新鲜事VIEW
		homeNewsArea = (RelativeLayout) findViewById(R.id.homeNewsArea);
		homeNewsLV = (ListView) findViewById(R.id.homeNewsList);
		// 取得我的主页VIEW
		homePageArea = (LinearLayout) findViewById(R.id.homePageArea);
		// 取得我的好友VIEW
		homeFriendArea = (LinearLayout) findViewById(R.id.homeFriendArea);
		// 设置我的好友
		homeFriendLV = (ListView) findViewById(R.id.homeFriendList);

		// 添加“读取更多按钮”
		liveFoot = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.live_foot, null, false);

		// 初始化-删除全部VIEW
		liveUserHome = (LinearLayout) findViewById(R.id.liveUserHome);
		liveUserHome.removeAllViews();

		// 默认显示我的主页
		liveUserHome.addView(homePageArea);

		// 我的主页
		TextView homePage = (TextView) findViewById(R.id.homePage);
		homePage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTabInit(R.id.homePageLine);

				liveUserHome.addView(homePageArea);
			}
		});

		// 留言提醒
		homeMsgSA = new LiveHomeMsgSA(context, liveHomeMsgList, R.layout.home_msg_list, new String[] {}, new int[] {},
				store.getString("id", null));
		homeMsgLV.addFooterView(liveFoot);
		homeMsgLV.setAdapter(homeMsgSA);
		homeMsgLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				final Map<String, Object> obj = liveHomeMsgList.get(arg2);
				if (!store.getString("id", null).equals(obj.get("uId").toString())) {
					Intent intent = new Intent();
					intent.setClass(context, LiveMessageActivity.class);

					bundle.putString("id", (String) obj.get("id"));
					bundle.putString("uId", store.getString("id", null));
					bundle.putString("fId", (String) obj.get("uId"));
					intent.putExtras(bundle);
					startActivityForResult(intent, Constant.REQUEST_CODE);
				}
			}
		});
		homeMsgLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && msgIndex == homeMsgSA.getCount()) {
					msgPage = msgPage + 1;
					bundle.putInt("page", msgPage);
					LiveMessage task = new LiveMessage(context, liveHomeMsgList, homeMsgSA, null,
							Constant.ACTION_SELECT, homeMsgLV, liveFoot);
					task.execute(bundle);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				// 计算最后可见条目的索引
				msgIndex = firstVisibleItem + visibleItemCount - 1;
			}
		});
		bundle.putInt("page", 0);
		LiveMessage task = new LiveMessage(context, liveHomeMsgList, homeMsgSA, null, Constant.ACTION_SELECT,
				homeMsgLV, liveFoot);
		task.execute(bundle);

		// 初始化新鲜事
		newsSA = new LiveNewsSA(context, liveHomeNewsList, R.layout.topic_list, new String[] {}, new int[] {},
				store.getString("userId", null));
		homeNewsLV.addFooterView(liveFoot);
		homeNewsLV.setAdapter(newsSA);
		homeNewsLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Map<String, Object> obj = liveHomeNewsList.get(arg2);
				Intent intent = new Intent();
				intent.setClass(context, LiveReplayViewActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("id", (String) obj.get("id"));
				bundle.putString("uId", (String) obj.get("uId"));
				bundle.putString("matchId", (String) obj.get("matchId"));
				bundle.putString("userId", (String) obj.get("userId"));
				bundle.putString("userPic", (String) obj.get("userPic"));
				bundle.putString("userPicVer", (String) obj.get("userPicVer"));
				bundle.putString("userSex", (String) obj.get("userSex"));
				bundle.putString("userCity", (String) obj.get("userCity"));
				bundle.putString("userAge", (String) obj.get("userAge"));
				bundle.putString("title", (String) obj.get("title"));
				bundle.putString("content", (String) obj.get("content"));
				bundle.putString("updateDate", (String) obj.get("updateDate"));
				intent.putExtras(bundle);
				startActivityForResult(intent, 0);
			}
		});
		homeNewsLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && newsIndex == newsSA.getCount()) {
					newsPage = newsPage + 1;
					bundle.putInt("page", newsPage);
					LiveTopic task = new LiveTopic(context, liveHomeNewsList, newsSA, null, Constant.ACTION_SELECT,
							homeNewsLV, liveFoot);
					task.execute(bundle);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				// 计算最后可见条目的索引
				newsIndex = firstVisibleItem + visibleItemCount - 1;
			}
		});
		TextView homeNews = (TextView) findViewById(R.id.homeNews);
		homeNews.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTabInit(R.id.homeNewsLine);

				liveUserHome.addView(homeNewsArea);

				bundle.putInt("page", 0);

				LiveTopic task = new LiveTopic(context, liveHomeNewsList, newsSA, null, Constant.ACTION_SELECT,
						homeNewsLV, liveFoot);
				task.execute(bundle);

				// 刷新新鲜事
				Button reloadBtn = (Button) findViewById(R.id.reloadBtn);
				reloadBtn.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						bundle.putInt("page", 0);
						LiveTopic task = new LiveTopic(context, liveHomeNewsList, newsSA, null, Constant.ACTION_SELECT,
								homeNewsLV, liveFoot);
						task.execute(bundle);
					}
				});
			}
		});

		// 初始化-我的好友
		homeFriendSA = new LiveHomeFriendSA(context, liveFriendList, R.layout.home_friend_list, new String[] {},
				new int[] {}, store.getString("userId", null));
		homeFriendLV.addFooterView(liveFoot);
		homeFriendLV.setAdapter(homeFriendSA);
		homeFriendLV.setOnItemClickListener(new OnItemClickListener() {

			// 从当前画面到比分明细画面跳转，并传值。
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Map<String, Object> obj = liveFriendList.get(arg2);
				Intent intent = new Intent();
				intent.setClass(context, LiveFriendHomeActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("f", (String) obj.get("f"));
				bundle.putString("uId", (String) obj.get("uId"));
				bundle.putString("userId", (String) obj.get("userId"));
				bundle.putString("userSign", (String) obj.get("userSign"));
				bundle.putString("userSex", (String) obj.get("userSex"));
				bundle.putString("userAge", (String) obj.get("userAge"));
				if (!"null".equals(obj.get("userCity").toString())) {
					bundle.putString("userCity", (String) obj.get("userCity"));
				} else {
					bundle.putString("userCity", context.getResources().getString(R.string.user_city_no));
				}
				bundle.putString("userPic", (String) obj.get("userPic"));
				bundle.putString("userPicVer", (String) obj.get("userPicVer"));
				intent.putExtras(bundle);
				context.startActivity(intent);
			}
		});
		homeFriendLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && friendIndex == homeFriendSA.getCount()) {
					friendPage = friendPage + 1;
					bundle.putInt("page", friendPage);
					// 我的好友
					String url = Constant.SITE_URL + "live_friend.php?locale=" + LiveTools.getLocaleLanguage();
					url += "&uId=" + bundle.getString("uId") + "&action=" + Constant.ACTION_SELECT + "&fState="
							+ Constant.ACTION_ALLOW;
					bundle.putString("action", Constant.ACTION_SELECT);
					LiveFriend task = new LiveFriend(context, url, liveFriendList, homeFriendSA, null, null,
							homeFriendLV, liveFoot);
					task.execute(bundle);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				// 计算最后可见条目的索引
				friendIndex = firstVisibleItem + visibleItemCount - 1;
			}
		});
		// 我的好友
		TextView homeFriend = (TextView) findViewById(R.id.homeFriend);
		homeFriend.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTabInit(R.id.homeFriendLine);

				liveUserHome.addView(homeFriendArea);

				// 我的好友
				String url = Constant.SITE_URL + "live_friend.php?locale=" + LiveTools.getLocaleLanguage();
				url += "&uId=" + bundle.getString("uId") + "&action=" + Constant.ACTION_SELECT + "&fState="
						+ Constant.ACTION_ALLOW;
				bundle.putInt("page", 0);
				bundle.putString("action", Constant.ACTION_SELECT);
				LiveFriend task = new LiveFriend(context, url, liveFriendList, homeFriendSA, null, null, homeFriendLV,
						liveFoot);
				task.execute(bundle);
			}
		});
	}

	private void changeTabInit(int tab) {
		// 隐藏下划线
		TextView homeNewsLine = (TextView) findViewById(R.id.homeNewsLine);
		homeNewsLine.setVisibility(Constant.INVISIBLE);
		TextView homePageLine = (TextView) findViewById(R.id.homePageLine);
		homePageLine.setVisibility(Constant.INVISIBLE);
		TextView homeFriendLine = (TextView) findViewById(R.id.homeFriendLine);
		homeFriendLine.setVisibility(Constant.INVISIBLE);

		TextView view = (TextView) findViewById(tab);
		view.setVisibility(Constant.VISIBLE);

		liveUserHome.removeAllViews();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == Constant.REQUEST_CODE) {
			if (resultCode != RESULT_CANCELED) {
				if (Constant.RELOAD.equals(data.getAction())) {
					if (findViewById(R.id.homeNewsArea) != null) {
						bundle.putInt("page", 0);
						LiveTopic task = new LiveTopic(context, liveHomeNewsList, newsSA, null, Constant.ACTION_SELECT,
								homeNewsLV, liveFoot);
						task.execute(bundle);
					} else if (findViewById(R.id.homePageArea) != null) {
						bundle.putInt("page", 0);
						LiveMessage task = new LiveMessage(context, liveHomeMsgList, homeMsgSA, null,
								Constant.ACTION_SELECT, homeMsgLV, liveFoot);
						task.execute(bundle);
					}
				}
			}
		}
	}
}